package com.itboye.jigongbao.activity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jigongbao.R;
import com.itboye.jigongbao.adpapter.ProvinceAdapter;
import com.itboye.jigongbao.app.MyApplication;
import com.itboye.jigongbao.base.BaseActivity;
import com.itboye.jigongbao.bean.CitysBean;
import com.itboye.jigongbao.utils.TitleBarUtils;
import com.itboye.jigongbao.widget.titlebarwidget.TranslucentActionBar;

import java.util.ArrayList;

/**
 * 省
 */
public class ProvinceActivity extends BaseActivity {
    TextView add_shap_title_tv, add_lift_tv;
    RelativeLayout lay_actionbar_left;
    ListView city_listview;
    private SQLiteDatabase database;
    private ArrayList<CitysBean> mCityNames;
    ProvinceAdapter adapter;
    String id;
    String name;
    String type;
    TranslucentActionBar trans_action_bar_hometown;
    String address;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_actionbar_left:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_city;
    }

    @Override
    protected void initData() {
        new TitleBarUtils().initTitlebarStyle1(this, trans_action_bar_hometown, "选择地址", R.drawable.img_back, "", 0, "");
        type = getIntent().getStringExtra("type");
        if (type.equals("province")) {
            mCityNames = MyApplication.Companion.getCityDBManager().getProvinces();
        } else if (type.equals("city")) {
            String provinceId = getIntent().getStringExtra("provinceId");
            mCityNames = MyApplication.Companion.getCityDBManager().getCities(provinceId);
        } else if (type.equals("area")) {
            String cityId = getIntent().getStringExtra("cityId");
            mCityNames = MyApplication.Companion.getCityDBManager().getAreas(cityId);
        }
        adapter = new ProvinceAdapter(this, mCityNames, R.layout.city_ceshi);
        city_listview.setAdapter(adapter);
        city_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                name = mCityNames.get(position).getProvince();
                Intent intent = new Intent(ProvinceActivity.this, ProvinceActivity.class);
                if (type.equals("province")) {
                    ProvinceActivity.this.id = mCityNames.get(position).getProvinceId();
                    intent.putExtra("provinceId", ProvinceActivity.this.id);
                    intent.putExtra("province", mCityNames.get(position).getProvince());
                    intent.putExtra("type", "city");
                    startActivity(intent);
                } else if (type.equals("city")) {
                    ProvinceActivity.this.id = mCityNames.get(position).getCityId();
                    intent.putExtra("cityId", ProvinceActivity.this.id);
                    intent.putExtra("type", "area");
                    String city = mCityNames.get(position).getCityName();
                    if (mCityNames.get(position).getCityName().equals("市辖区") || mCityNames.get(position).getCityName().equals("县") || mCityNames.get(position).getCityName().equals("市")) {
                        city="";
                    } else {

                    }
                    intent.putExtra("city", getIntent().getStringExtra("province") + "" + city);
                    startActivity(intent);
                } else if (type.equals("area")) {
                    address = getIntent().getStringExtra("city") + mCityNames.get(position).getAreaName();
                    MineActivity.Companion.getMinActivity().getLoginInfo().setHometown(address);
                }
                finish();
            }
        });
    }
}
